<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\NodeService;
use app\common\facade\Arr;
use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\services\JobService;
use app\common\services\PublishJobService;
use app\common\tools\ExportExcel;


/**
 * 兼职报表
 * Class Index
 * @package app\admin\controller
 */
class PaltJobStat extends Controller
{
    public $table = 'Jobs';
    /**
     * 兼职统计
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {

        $result['total'] = Db::name($this->table)->where('status!=0')->count();//全部兼职
        $result['beaudite']=Db::name($this->table)->where('status=1')->count();//待审核
        $result['pass']=Db::name($this->table)->where('status=2')->count();//审核通过兼职
        $result['deny']=Db::name($this->table)->where('status=3')->count();//审核拒绝
        $result['work']=Db::name($this->table)->where('status=4')->count();//工作中兼职
        $result['complete']=Db::name($this->table)->where('status=5')->count();//已完成兼职
        $this->assign('result',$result);

        $start_time= $this->request->param('start_time');
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }

        }else{
            $today=date("Y-m-d");
            $start=strtotime($today." 00:00:00");
            $end=strtotime($today. " 23:59:59");
        }

        $st[]=['status','NEQ',0];
        $st[]=['created_at','between time',[$start, $end]];
        $query = $this->_query($this->table)
                ->where($st)
                ->like('title')
                ->equal('id,mobile,work_type,is_top,contacts');
        $query->order('id desc')->page(); 
       
    }

        /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {



        $this->clist = Db::name('JobsType')->field("id,name")->select();
        foreach ($data as &$vo) {
            $vo['salary_unitCh'] = TransItem::transMoneyUnit(Arr::get($vo, 'salary_unit'));
            $vo['settlement_typeCh'] = TransItem::transSettlement(Arr::get($vo, 'settlement_type'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['add_time'] = date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['work_type']) $vo['cate'] = $cate;

        }
    }

    /**
     * 兼职日报表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function statdata(){

        $this->title="兼职日报表";

       
        $start_time= $this->request->param('start_time');
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }
            $data[]=['for_time','between time',[$start, $end]];
        }
        $data[]=['type','EQ',1];

        $query = $this->_query("platform_job_stat")
                ->where($data)
                ->like('id')
                ->equal('id,for_time');
        $query->order('id desc')->page(); 
    }

       /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _statdata_page_filter(&$data)
    {
        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月d日",$vo['for_time']);

        }
    }


    //每个代理商数据统计
    public function datelist(){
        
        $id =$this->request->param('id');
        $result=DB::name("platform_job_stat")->where('id',$id)->find();
        $this->assign('result',$result);

        $data[]=['js.type','EQ',1];
        $data[]=['js.for_time_start',"EQ",$result['for_time_start']];
        $data[]=['js.for_time_end',"EQ",$result['for_time_end']];
       
        $query = $this->_query("jobs_stat")
                ->alias('js')
                ->join("system_agent_user sau",'js.aid=sau.id')
                ->where($data)
                ->field("js.*,sau.username")
                ->like('js.for_time')
                ->equal('js.for_time');
        $query->order('js.id desc')->page(); 


    }


       /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _datelist_page_filter(&$data)
    {
        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月d日",$vo['for_time']);

        }
    }
     

    //兼职日报表详情
    public function detail(){

        $id =$this->request->param('id');
        $total=DB::name("jobsStat")->where('id',$id)->find();
        $this->assign('total',$total);


        $st[]=['provinces_id','EQ',$total['provinces_id']];
        $st[]=['city_id','EQ',$total['city_id']];
        $st[]=['status','NEQ',0];
        $st[]=['created_at','between time',[$total['for_time_start'], $total['for_time_end']]];

        $query = $this->_query($this->table)
                ->where($st)
                ->like('title')
                ->equal('title');
        $query->order('id desc')->page();  

    }

     /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _detail_page_filter(&$data)
    {

        $this->clist = Db::name('JobsType')->field("id,name")->select();
        foreach ($data as &$vo) {
            $vo['salary_unitCh'] = TransItem::transMoneyUnit(Arr::get($vo, 'salary_unit'));
            $vo['settlement_typeCh'] = TransItem::transSettlement(Arr::get($vo, 'settlement_type'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['add_time'] = date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['work_type']) $vo['cate'] = $cate;

        }
    }


    /**
     * 兼职月报表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function statmonth(){

        $this->title="兼职月报表";

        $start_time= $this->request->param('start_time');
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }
            $data[]=['for_time','between time',[$start, $end]];

        }
        $data[]=['type','EQ',2];
        $query = $this->_query("platform_job_stat")
                ->where($data)
                ->like('id')
                ->equal('id,for_time');
        $query->order('id desc')->page(); 
    }


        /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _statmonth_page_filter(&$data)
    {

        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月",$vo['for_time_end']);

        }
    }

     //每个代理商每月数据统计
    public function monthlist(){
        
        $id =$this->request->param('id');
        $result=DB::name("platform_job_stat")->where('id',$id)->find();
        $this->assign('result',$result);

        $data[]=['js.type','EQ',2];
        $data[]=['js.for_time_start',"EQ",$result['for_time_start']];
        $data[]=['js.for_time_end',"EQ",$result['for_time_end']];
       
        $query = $this->_query("jobs_stat")
                ->alias('js')
                ->join("system_agent_user sau",'js.aid=sau.id')
                ->where($data)
                ->field("js.*,sau.username")
                ->like('js.for_time')
                ->equal('js.for_time');
        $query->order('js.id desc')->page(); 


    }

       /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _monthlist_page_filter(&$data)
    {
        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月",$vo['for_time']);

        }
    }


    //兼职月报表详情
    public function mdetail(){

        $id =$this->request->param('id');
        $total=DB::name("jobsStat")->where('id',$id)->find();
        $this->assign('total',$total);


        $st[]=['provinces_id','EQ',$total['provinces_id']];
        $st[]=['city_id','EQ',$total['city_id']];
        $st[]=['status','NEQ',0];
        $st[]=['created_at','between time',[$total['for_time_start'], $total['for_time_end']]];

        $query = $this->_query($this->table)
                ->where($st)
                ->like('title')
                ->equal('title');
        $query->order('id desc')->page();  

    }

     /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _mdetail_page_filter(&$data)
    {

        $this->clist = Db::name('JobsType')->field("id,name")->select();
        foreach ($data as &$vo) {
            $vo['salary_unitCh'] = TransItem::transMoneyUnit(Arr::get($vo, 'salary_unit'));
            $vo['settlement_typeCh'] = TransItem::transSettlement(Arr::get($vo, 'settlement_type'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['add_time'] = date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['work_type']) $vo['cate'] = $cate;

        }
    }




  

}
